@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
echo   \\\       \\   \\\          \\\\\\\\                                         
echo   \\\       \\   \\\        \\\\\\\\\\\                                        
echo    \\       \\   \\\         \\\     \\\          \\     \\\\ \\      \\       
echo    \\       \\   \\\          \\      \\    \\\\\\\\    \\\\\\\\   \\\\\\\     
echo    \        \\    \           \\     \\\   \\\   \\\   \\    \\   \\\    \\    
echo   \\        \\    \           \\\\\\\\\    \\     \\   \\         \\      \\   
echo   \\        \\    \           \\\\\\\\\\   \\     \\    \\\\\\\   \\\\\\\\\\   
echo   \\        \\    \\          \\      \\\  \\     \\      \\\\\\ \\\    \\\    
echo   \\       \\\    \\          \\       \\  \\     \\           \  \\      \\   
echo   \\\     \\\    \\\          \\      \\\  \\     \\  \\\     \\  \\     \\\   
echo    \\\\\\\\\\    \\\         \\\\\\\\\\\\   \\\\\\\\\  \\\\\\\\\   \\\\\\\\    
echo     \\\\\\\       \         \\\\\\\\\\\      \\\\\ \    \\\\\\      \\\\\\
echo Get ready to start building your frontend development journey.
echo Copyright (c) 2024 Carl.Cui. All Rights Reserved.
set nodePath=
set nvmPath=
set nvmList=
set nvmUrl=https://github.com/coreybutler/nvm-windows/releases/download/1.1.12/nvm-setup.exe
set vsCodeUrl=https://vscode.download.prss.microsoft.com/dbazure/download/stable/384ff7382de624fb94dbaf6da11977bba1ecd427/VSCodeUserSetup-x64-1.94.2.exe
:start
for /f "delims=" %%i in ('where node') do (
    set nodePath=%%i
    break
)
for /f "delims=" %%i in ('where nvm') do (
    set nvmPath=%%i
    break
)
set exist_normal_node=false
set exist_nvm_node=false
if not defined nodePath (
    set exist_normal_node=false
) else (
    set exist_normal_node=true
)
if not defined nvmPath (
    set exist_nvm_node=false
) else (
    set exist_nvm_node=true
)
if "%exist_normal_node%"=="true" (
    goto :already_install_node
) else if "%exist_nvm_node%"=="true" (
    goto :go_nvm
) else (
    goto :install_node
)
:install_node
echo Error: Node.js is not installed,Do you want to install it?
pause
echo You want install NVM(Node version management tool) or normal Node.js?
:re_input
set /p node=(NVM/Node):
if "!node: =%U!" == "nvm" (
    echo NVM downloading...
    powershell -Command "Invoke-WebRequest -Uri %nvmUrl%"
    echo NVM download successfully!
    start %CD%/nvm-setup.exe
    pause
    goto :start
) else if "!node: =%U!" == "node" (
    goto :go_node
) else (
    echo Invalid input. Please enter either 'NVM' or 'Node'.
    goto :re_input
)
:go_nvm
nvm install latest
echo The available version numbers are:
nvm list
set /p nodeVersionNumber=Please enter your version number:
nvm use !nodeVersionNumber!
pause
goto :start
:go_node
echo Attempting to download and install it.
powershell -Command "Invoke-WebRequest https://nodejs.org/dist/v20.18.0/node-v20.18.0-win-x64.zip -OutFile node.zip"
powershell -Command "Expand-Archive -Path node.zip -DestinationPath %TEMP%\node"
setx PATH "!PATH!;%TEMP%\node\node-v20.18.0-win-x64"
set nodePath=%TEMP%\node\node-v20.18.0-win-x64\node.exe
if not exist "!nodePath!" (
    echo Failed to install Node.js. Please manually download and install it.
    pause
    goto :eof
)
else (
    echo Node already install!
)
:already_install_node
for /f "tokens=* delims=" %%n in ('"%nodePath%" -v 2^>nul') do (
    set nodeVersion=%%n
)
if not defined nodeVersion (
    echo Error: Unknown error.
    goto :end_cmd
)
if defined nodeVersion (
    echo Node.js is already installed on your computer.
    echo Node version: !nodeVersion!
)
echo If you want to install VSCode?
:callBack
set /p installVsCode=(YES/NO):
if "!installVsCode: =%U!" == "yes" (
    start %vsCodeUrl%
    pause
    goto :createProject
) else if "!installVsCode: =%U!" == "no" (
    goto :createProject
) else (
    echo Invalid input. Please enter either 'YES' or 'NO'.
    goto :callBack
)
:createProject
echo Do you want to create a new project? (Based on Node)
:prompt
set /p type=(YES/NO):
if "!type: =%U!" == "yes" (
    set scriptDir=%~dp0 
    echo !scriptDir!
    npm i yarn -g
    yarn create vite
    echo Project created successfully!
    pause
) else if "!type: =%U!" == "no" (
    echo Welcome to next using!
    goto :end_cmd
) else (
    echo Invalid input. Please enter either 'YES' or 'NO'.
    goto :prompt
)
:end_cmd
pause
endlocal